package com.jr.controller;

import com.jr.entry.BookInfo;
import com.jr.entry.Users;
import com.jr.pojo.BookType;
import com.jr.pojo.IsBorrwo;
import com.jr.service.UsersService;
import com.jr.util.PageHelper;
import com.sun.org.apache.bcel.internal.generic.I2F;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private int I = 1;

    @Autowired
    private UsersService usersService;


    @RequestMapping("/login")
    @ResponseBody
    public boolean login( String user_code, String password){
        Users users = new Users();
        users.setUser_code(user_code);
        users.setPassword(password);
        Users login = usersService.login(users);
        if (login==null){
            return false;
        }else {
            return true;
        }

    }

    @RequestMapping("/loadBorrwo")
    @ResponseBody
    public List<IsBorrwo> loadIsBorrow(){
        List<IsBorrwo> borrwo = usersService.findByIsBorrwo();

        return borrwo;
    }

    @RequestMapping("/BookType")
    @ResponseBody
    public List<BookType> loadBookType(){
        List<BookType> byBookType = usersService.findByBookType();
        return byBookType;
    }

 @RequestMapping("/show")
 @ResponseBody
 public PageHelper selectBookInfo(String bookType,String isBorrow,String bookName,String indexPage,String showCount){
     BookInfo bookInfo = new BookInfo();
     PageHelper pageHelper = new PageHelper();
     if (bookType!=""){
         bookInfo.setBook_type(bookType);
     }
     if (bookName!=""){
         bookInfo.setBook_name(bookName);
     }
     if (isBorrow!=""){
         bookInfo.setIs_borrow(isBorrow);
     }
     if (showCount!=null&&!showCount.isEmpty()) {
         pageHelper.setShowCount(Integer.parseInt(showCount));
     }
     if (indexPage!=null&&!indexPage.isEmpty()){
         pageHelper.setIndexPage((Integer.parseInt(indexPage)-1)*Integer.parseInt(showCount));
     }
     int dataCount = usersService.findBookInfo(bookInfo).size();
     pageHelper.setDataCount(dataCount);
     pageHelper.setPageCount(dataCount % Integer.parseInt(showCount) == 0 ?
             dataCount / Integer.parseInt(showCount) :
             dataCount / Integer.parseInt(showCount) + 1);
     ArrayList<BookInfo> bookInfoByQueryAndIndex = usersService.findBookInfoByQueryAndIndex(bookInfo, pageHelper);
     pageHelper.setList(bookInfoByQueryAndIndex);
     System.out.println(pageHelper.toString());
     return pageHelper;
 }
 /*
 * @RequestMapping("/show")
 @ResponseBody
 public List<BookInfo> selectBookInfo(String bookType,String isBorrow,String bookName){
     BookInfo bookInfo = new BookInfo();
     if (bookType!=""){
         bookInfo.setBook_type(bookType);
     }
     if (bookName!=""){
         bookInfo.setBook_name(bookName);
     }
     if (isBorrow!=""){
         bookInfo.setIs_borrow(isBorrow);
     }
     return usersService.findBookInfo(bookInfo);
 }
 * */

    @RequestMapping("/add")
    @ResponseBody
    public boolean add(String book_code,String book_name,String book_type,String book_author,String publish_press,String publish_date,String is_borrow,String createBy,String creation_time,String last_updatetime){
        BookInfo bookInfo = new BookInfo(book_code,book_name,book_type,book_author,publish_date,publish_press,is_borrow,createBy,creation_time,last_updatetime);
        return usersService.addBookInfo(bookInfo);
    }
    @RequestMapping("/remove")
    @ResponseBody
    public boolean remove(String book_code){
        return usersService.removeBookInfo(book_code);
    }
    @RequestMapping("/update")
    @ResponseBody
    public boolean update(String is_borrow ,String book_code){
        BookInfo bookInfo =new BookInfo();
        bookInfo.setBook_code(book_code);
        if ("未借出".equals(is_borrow)){
            is_borrow="已借出";
        }
        bookInfo.setIs_borrow(is_borrow);
        System.out.println(book_code+":"+is_borrow);
        return usersService.updateBookInfo(bookInfo);
    }
}
